package com.wl.controller;

import com.wl.pojo.AIChat.ChatHistory;
import com.wl.service.AIChatService;
import com.wl.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/ai/chat")
public class AIChatController {

    @Autowired
    private AIChatService aiChatService;

    /**
     * 根据userId获取聊天记录
     */
    @GetMapping("/get/history")
    public Result getChatHistoryDatas(String userId) {
        List<ChatHistory> chatHistoryDatas = aiChatService.getChatHistoryDatas(userId);
        return Result.success(chatHistoryDatas);
    }

    /**
     * 修改聊天记录标题
     */
    @PostMapping("/update")
    public Result updateChatHistoryTitle(@RequestBody ChatHistory chatHistory) {
        aiChatService.updateChatHistoryTitle(chatHistory);
        return Result.success();
    }

    /**
     * 更新聊天记录内容
     */
    @PostMapping("/update/messages")
    public Result updateChatHistoryMessages(@RequestBody ChatHistory chatHistory) {
        aiChatService.updateChatHistoryMessages(chatHistory);
        return Result.success();
    }

    /**
     * 删除聊天记录
     */
    @DeleteMapping("/delete")
    public Result deleteChatHistory(@RequestParam Integer chatHistoryId) {
        aiChatService.deleteChatHistory(chatHistoryId);
        return Result.success();
    }

    /**
     * 保存聊天记录并立即返回
     */
    @PostMapping("/save")
    public Result saveChatHistory(@RequestBody ChatHistory chatHistory) {
        return Result.success(aiChatService.saveChatHistory(chatHistory));
    }

}
